/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view;

import static java.lang.System.exit;

import org.zzz.controller.UserController;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;

import javax.swing.*;

import org.zzz.view.teacherControl.teacherSelectFrame;
import org.zzz.view.teacherControl.teacherViewFrame;
import org.zzz.view.util.UiUtil;

/**
 *
 * @author AFun
 */
public class enterFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public enterFrame() {
        initComponents();
        init();         //初始化
    }
    private void init(){
        this.setTitle("请选择您的身份并登录");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
    }

    public enterFrame getEnterFrame(){
        return this;
    }


    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mima = new javax.swing.JPasswordField();
        zhanghao = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        denglu = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        shenfen = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mima.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        mima.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mimaActionPerformed(evt);
            }
        });

        zhanghao.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zhanghaoActionPerformed(evt);
            }
        });

        jLabel1.setText("账号");

        jLabel2.setText("密码");

        jLabel3.setText("身份");

        denglu.setText("登录");
        denglu.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dengluActionPerformed(evt);
            }
        });

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("欢迎来到ZZZ的学生考勤系统");

        shenfen.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "管理员", "教师", "学生" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(zhanghao, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(shenfen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(denglu)
                                .addGap(70, 70, 70)
                                .addComponent(jButton2))
                            .addComponent(mima, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(96, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zhanghao, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(shenfen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mima, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(denglu)
                    .addComponent(jButton2))
                .addGap(61, 61, 61))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void zhanghaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zhanghaoActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_zhanghaoActionPerformed

    private void mimaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mimaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mimaActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(this,"欢迎下次再来！");
        this.dispose();
        exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void dengluActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dengluActionPerformed
        // TODO add your handling code here:
        String zh=this.zhanghao.getText().trim();
        String ma=this.mima.getText().trim();
        String sf=this.shenfen.getSelectedItem().toString();
        Result<String> rs=uc.login(zh,ma);
        if(rs.getStatus()== ResultEnum.SUCCESS.getStatus()&&rs.getData().equals(sf)){
            this.dispose();
            if(sf.equals("管理员")){
                managerSelectFrame mf=new managerSelectFrame();
                mf.setVisible(true);
            }else if(sf.equals("教师")){
                teacherViewFrame tsf=new teacherViewFrame(this,zh);
                tsf.setVisible(true);
            }else if(sf.equals("学生")){
                studentFrame stuf=new studentFrame(zh);
                stuf.setEnterFrame(this);
                if(stuf.is_exist()){
                    stuf.setVisible(true);
                }
            }
        }else {
            JOptionPane.showMessageDialog(this, "输入有误，请重新输入");
            this.zhanghao.setText("");
            this.mima.setText("");
            this.zhanghao.requestFocus();
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(enterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(enterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(enterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(enterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new enterFrame().setVisible(true);
            }
        });
    }

    private UserController uc = new UserController();

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton denglu;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField mima;
    private javax.swing.JComboBox<String> shenfen;
    private javax.swing.JTextField zhanghao;
    // End of variables declaration//GEN-END:variables
}
